package com.zztl.infiniteBand.net.converter

import com.google.gson.Gson
import com.google.gson.TypeAdapter
import com.zztl.infiniteBand.util.LogUtils
import com.zztl.infiniteBand.util.rsa.RSAUtils

import java.io.IOException

import okhttp3.MediaType
import okhttp3.RequestBody
import retrofit2.Converter

/**
 * @desc
 * @auth ${user}
 * @time 2018/6/7  10:32
 */
class EncodeRequestConverter<T>(private val gson: Gson, private val adapter: TypeAdapter<T>) : Converter<T, RequestBody> {


    @Throws(IOException::class)
    override fun convert(value: T): RequestBody? {
        try {
            LogUtils.i("http", "请求参数===" + value.toString())
            val data = RSAUtils.encryptByPublicKey(value.toString())

            return RequestBody.create(MEDIA_TYPE, data)
        } catch (e: Exception) {
            e.printStackTrace()
            return null
        }

    }

    companion object {
        private val MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8")
    }

}
